<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索框区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div>
            <el-input clearable placeholder="请输入内容" v-model="queryInfo.query" @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col class="addBtn" :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-row>
        <el-table :data="goodsList" border stripe style="width: 100%">
          <el-table-column type="index"> </el-table-column>
          <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
          <el-table-column prop="goods_price" width="120" label="商品价格(元)"> </el-table-column>
          <el-table-column prop="goods_weight" width="100" label="商品重量"> </el-table-column>
          <el-table-column prop="add_time" width="200" label="创建时间">
            <template slot-scope="scope">
              {{ scope.row.add_time | format('yyyy-MM-dd hh:mm:ss') }}
            </template>
          </el-table-column>
          <el-table-column width="220" label="操作">
            <!-- 操作模板 -->
            <template slot-scope="scope">
              <el-button type="primary" @click="showEditDialog(scope.row)" icon="el-icon-edit" size="mini">编辑 </el-button>
              <el-button type="danger" @click="deleteGoods(scope.row.goods_id)" icon="el-icon-delete" size="mini">删除 </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- 分页功能 -->
      <el-row>
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </el-row>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品列表
      goodsList: [],
      queryInfo: {
        query: '',
        pagenum: 1, //当前页码
        pagesize: 10 //每页显示条数
      },
      total: 0 //数据总条数
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: response } = await this.$axios.get('goods', {
        params: this.queryInfo
      })
      // console.log(response)
      if (response.meta.status !== 200) return this.$message.error('获取商品列表失败')
      this.goodsList = response.data.goods
      this.total = response.data.total
      return this.$message.success(response.meta.msg)
    },
    showEditDialog() {},
    //删除商品
    async deleteGoods(id) {
      await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: response } = await this.$axios.delete('goods/' + id)
          if (response.meta.status !== 200) return this.$message.error('删除商品信息失败')
          this.getGoodsList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      //   console.log(`每页 ${newSize} 条`)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(`当前页: ${newPage}`)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    //前往添加商品页面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 15px;
}
.block {
  margin-top: 10px;
  display: flex;
  justify-content: center;
}
</style>
